package com.igeek.mygh.TTLQueue;

import org.springframework.amqp.core.*;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xlz;
 * @data 2022-08-09 19:10
 */
@Configuration
public class DelayedQueueConfig {
    //交换机
    public static final String DELAYED_EXCHANGE_NAME = "delayed_exchange";
    //队列
    public static final String DELAYED_QUEUE_NAME = "delayed_queue";
    //routingkey
    public static final String DELAYED_ROUTING_KEY = "delayed_routing_key";
    //声明基于插件的延迟交换机
    @Bean
    public CustomExchange delayedExchange(){
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type","direct");
        return new CustomExchange(DELAYED_EXCHANGE_NAME,"x-delayed-message",true,false,arguments);
    }

    //声明延迟队列
    @Bean
    public Queue delayedQueue(){
        return QueueBuilder.durable(DELAYED_QUEUE_NAME).build();
    }
    //绑定routingkey
    @Bean
    public Binding delayedQueueBinding(
            @Qualifier("delayedQueue")Queue delayedQueue,
            @Qualifier("delayedExchange")CustomExchange delayedExchange
    ){
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
    }
}
